package com.itbour.service.trade.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.itbour.com.page.JspPage;
import com.itbour.com.page.Page;
import com.itbour.common.util.StringUtils;
import com.itbour.dao.trade.ItemsDao;
import com.itbour.dto.com.UserInfoDto;
import com.itbour.dto.trade.ItemInfoDto;
import com.itbour.dto.trade.PrintParamInfoDto;
import com.itbour.service.trade.ItemsService;
import com.opensymphony.xwork2.ActionContext;


/**
* 商品
* 
* copyright   itbour team 2016
* @author     xc
*
*/

public class ItemsServiceImpl implements ItemsService {

	private ItemsDao itemsDao;
	
	/**
	 * @param countKey
	 *            查询该表所有记录行的SQL语句ID
	 * @param sqlKey
	 *            需要查询记录的SQL语句ID
	 * @param param
	 *            传递给查询语句的参数
	 * @param pageNo
	 *            需要查询的实际页
	 * @param pageSize
	 *            每页记录行数
	 * @return page对象
	 */
	public Page itemsPgQuery(Map<String,Object> param, Integer pageNo, Integer pageSize) {
		try {
			Page result = null;
			// 实际起始位置
			int skipResults = JspPage.getStartOfPage(pageNo, pageSize);
			int totalSize = itemsDao.selectItemsCnt(param);
			
			while(totalSize >0 && totalSize <= skipResults){
				skipResults= skipResults - pageSize;
				pageNo = pageNo -1;
			}
			
			// 分页检索
			param.put("pageSize", pageSize);
			param.put("skipResults", skipResults);
			List<ItemInfoDto> data = itemsDao.selectItemsInfo(param);

			result = new JspPage(skipResults, totalSize, pageSize, data);
			return result;
		} finally {
			//clearSelectDataSource(dataSource);
		}
	}

	public List<ItemInfoDto> selectItemsInfo(Map<String,Object> param) {
		return itemsDao.selectItemsInfo(param);
	}
	
	public void delItem(String delIds, String updBy) {
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("ids", StringUtils.listToString2(StringUtils.stringToList2(delIds)));
		param.put("update_by", updBy);
		List<ItemInfoDto> data = itemsDao.selectItemsInfo(param);// 查找
		if(data!=null && data.size()>0) {
			// 删除
			itemsDao.deleteItemInfoLogic(param);
			
			// 反向更新design信息
			String designIds = "";
			for(int i=0; i<data.size(); i++) {
				String id = String.valueOf(data.get(i).getDesign_id());
				if(id!=null) {
					if (designIds.length()>0) {
						designIds += ",";
					}
					designIds += id;
				}
			}
			param.put("design_ids", designIds);
			itemsDao.clrDesignRelItem(param);
		}
		
	}

	public void addItem(ItemInfoDto itemInfo) {
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("item_no", itemInfo.getItem_no());
		param.put("item_name", itemInfo.getItem_name());
		param.put("desc", itemInfo.getDesc());
		param.put("type_id", itemInfo.getType_id());
		param.put("state", itemInfo.getState());
		param.put("design_id", itemInfo.getDesign_id());
		param.put("theme_thumb", itemInfo.getTheme_thumb());
		param.put("thumbs", itemInfo.getThumbs());
		param.put("update_by", itemInfo.getUpdate_by());
		itemsDao.insertItemInfo(param);
	}
	
	public void updItem(Map<String,Object> param) {
		itemsDao.updateItemInfoById(param);
	}

	public List<PrintParamInfoDto> selectPpInfo(Map<String,Object> param){
		return itemsDao.selectPpInfoForItem(param);
	}
	
	public void delRelPpInfo(Map<String,Object> param) {
		itemsDao.deletePrintRelInfoLogic(param);
	}
	
	public void addRelPpInfo(Map<String,Object> param) {
		itemsDao.insertPrintRelInfo(param);
	}
	
	public void clrRelDesign(Map<String,Object> param) {
		List<ItemInfoDto> data = itemsDao.selectItemsInfo(param);
		if(data!=null && data.size()>0) {
			itemsDao.clrRelDesign(param);
			String dIds = "";
			for(int i=0; i<data.size(); i++) {
				String id = String.valueOf(data.get(i).getDesign_id());
				if (i>0) {
					dIds += ",";
				}
				dIds += id;
			}
			// 反向更新design信息
			param.put("design_ids", dIds);
			itemsDao.clrDesignRelItem(param);
		}
	}
	
	public ItemsDao getItemsDao() {
		return itemsDao;
	}

	public void setItemsDao(ItemsDao itemsDao) {
		this.itemsDao = itemsDao;
	}

}
